﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CDV.MC.WebAPI.TaskExecution.Filter
{
    /// <summary>
    /// 校验入参特性
    /// </summary>
    public class ValidateModelAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 校验入参
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.ActionArguments.Count == 0 
                || !context.ActionArguments.TryGetValue("routeID", out object objRouteID))
            {
                base.OnActionExecuting(context);
                return;
            }

            if (objRouteID != null 
                && int.TryParse(objRouteID.ToString(), out int routeID) 
                && routeID <= 0)
            {
                context.Result = new BadRequestResult();
            }
        }
    }
}
